package com.bwie.xujiahaonewstitle;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bwie.newstitlexujiahao.R;
import com.bwie.xujiahaonewstitle.fragment.FindFragment;
import com.bwie.xujiahaonewstitle.fragment.FocusFragment;
import com.bwie.xujiahaonewstitle.fragment.HomeFragment;
import com.bwie.xujiahaonewstitle.fragment.MeFragment;
import com.bwie.xujiahaonewstitle.help.NetWorkUtil;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tencent.connect.UserInfo;
import com.tencent.connect.common.Constants;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private RelativeLayout homeRelativeLayout,findRelativeLayout,focusRelativeLayout,meRelativeLayout;
    private ImageView homeImageView,findImageView,focusImageView,meImageView;
    private TextView homeTextView,findTextView,fcousTextView,meTextView;
    private FindFragment mFindFragment;
    private FocusFragment mFocusFragment;
    private MeFragment mMeFragment;
    public static Tencent mTencent;
    public static IUiListener loginListener;
    private IUiListener userInfoListener;
    private static final String APPID = "1105927999";
    private String mNickName;
    private String mMNickName;
    private String mFigureurlQq2;
    private ImageLoader mImageLoader;
    private FragmentTransaction mFragmentTransaction;
    private HomeFragment mHomeFragment;
    private int mResponse;
    private View mView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        mView = View.inflate(this, R.layout.activity_main, null);
        setContentView(mView);
        //判断网络是否连接
        if (!NetWorkUtil.isConn(getApplicationContext())) {
            NetWorkUtil.setNetworkMethod(this);
        } else {
            initQqLogin();
            initViews();
            mHomeFragment = new HomeFragment();
            getSupportFragmentManager().beginTransaction().add(R.id.fragmentlayout, mHomeFragment).commit();
        }
    }

    /*获取资源ID*/
    private void initViews() {
        homeRelativeLayout = (RelativeLayout) findViewById(R.id.homeRelativeLayout);
        findRelativeLayout = (RelativeLayout) findViewById(R.id.findRelativeLayout);
        focusRelativeLayout = (RelativeLayout) findViewById(R.id.focusRelativeLayout);
        meRelativeLayout = (RelativeLayout) findViewById(R.id.meRelativeLayout);
        homeImageView = (ImageView) findViewById(R.id.homeImageView);
        findImageView = (ImageView) findViewById(R.id.findImageView);
        focusImageView = (ImageView) findViewById(R.id.focusImageView);
        meImageView = (ImageView) findViewById(R.id.meImageView);
        homeTextView = (TextView) findViewById(R.id.homeTextView);
        findTextView = (TextView) findViewById(R.id.findTextView);
        fcousTextView = (TextView) findViewById(R.id.focusTextView);
        meTextView = (TextView) findViewById(R.id.meTextView);
        homeRelativeLayout.setOnClickListener(this);
        findRelativeLayout.setOnClickListener(this);
        focusRelativeLayout.setOnClickListener(this);
        meRelativeLayout.setOnClickListener(this);
        homeImageView.setEnabled(false);
        homeTextView.setTextColor(Color.RED);
        mImageLoader = ImageLoader.getInstance();
    }

    @Override
    public void onClick(View v) {
        mFragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideorshow(mFragmentTransaction);
        switch (v.getId()) {
            case R.id.homeRelativeLayout:
                homeImageView.setEnabled(false);
                homeTextView.setTextColor(Color.RED);
                findImageView.setEnabled(true);
                findTextView.setTextColor(Color.BLACK);
                focusImageView.setEnabled(true);
                fcousTextView.setTextColor(Color.BLACK);
                meImageView.setEnabled(true);
                meTextView.setTextColor(Color.BLACK);
                if (mHomeFragment == null) {
                    mHomeFragment = new HomeFragment();
                    mFragmentTransaction.add(R.id.fragmentlayout, mHomeFragment);
                } else {
                    mFragmentTransaction.show(mHomeFragment);
                }
                break;
            case R.id.findRelativeLayout:
                homeImageView.setEnabled(true);
                homeTextView.setTextColor(Color.BLACK);
                findImageView.setEnabled(false);
                findTextView.setTextColor(Color.RED);
                focusImageView.setEnabled(true);
                fcousTextView.setTextColor(Color.BLACK);
                meImageView.setEnabled(true);
                meTextView.setTextColor(Color.BLACK);
                if (mFindFragment == null) {
                    mFindFragment = new FindFragment();
                    mFragmentTransaction.add(R.id.fragmentlayout, mFindFragment);
                } else {
                    mFragmentTransaction.show(mFindFragment);
                }
                break;
            case R.id.focusRelativeLayout:
                homeImageView.setEnabled(true);
                homeTextView.setTextColor(Color.BLACK);
                findImageView.setEnabled(true);
                findTextView.setTextColor(Color.BLACK);
                focusImageView.setEnabled(false);
                fcousTextView.setTextColor(Color.RED);
                meImageView.setEnabled(true);
                meTextView.setTextColor(Color.BLACK);
                if (mFocusFragment == null) {
                    mFocusFragment = new FocusFragment();
                    mFragmentTransaction.add(R.id.fragmentlayout, mFocusFragment);
                } else {
                    mFragmentTransaction.show(mFocusFragment);
                }
                break;
            case R.id.meRelativeLayout:
                homeImageView.setEnabled(true);
                homeTextView.setTextColor(Color.BLACK);
                findImageView.setEnabled(true);
                findTextView.setTextColor(Color.BLACK);
                focusImageView.setEnabled(true);
                fcousTextView.setTextColor(Color.BLACK);
                meImageView.setEnabled(false);
                meTextView.setTextColor(Color.RED);
                if (mMeFragment == null) {
                    mMeFragment = new MeFragment();
                    mFragmentTransaction.add(R.id.fragmentlayout, mMeFragment);
                } else {
                    mFragmentTransaction.show(mMeFragment);
                }
                break;
            default:
                break;
        }
        mFragmentTransaction.commit();
    }

    //利用事务来实现fragment的显示隐藏
    private void hideorshow(FragmentTransaction fragmentTransaction) {
        if (mHomeFragment != null) {
            fragmentTransaction.hide(mHomeFragment);
        }
        if (mFindFragment != null) {
            fragmentTransaction.hide(mFindFragment);
        }
        if (mFocusFragment != null) {
            fragmentTransaction.hide(mFocusFragment);
        }
        if (mMeFragment != null) {
            fragmentTransaction.hide(mMeFragment);
        }
    }

    //初始化QQ登录分享的需要的资源
    private void initQqLogin() {
        mTencent = Tencent.createInstance(APPID, MainActivity.this);
        //创建QQ登录回调接口
        loginListener = new IUiListener() {
            @Override
            public void onComplete(Object o) {
                //登录成功后回调该方法
                JSONObject jo = (JSONObject) o;
                Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                Log.i("miaojx:", jo.toString());
                String openID;
                try {
                    openID = jo.getString("openid");
                    String accessToken = jo.getString("access_token");
                    String expires = jo.getString("expires_in");
                    mTencent.setOpenId(openID);
                    mTencent.setAccessToken(accessToken, expires);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(UiError uiError) {
                //登录失败后回调该方法
                Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                Log.e("LoginError:", uiError.toString());
            }
            @Override
            public void onCancel() {
                //取消登录后回调该方法
                Toast.makeText(MainActivity.this, "取消登录", Toast.LENGTH_SHORT).show();
            }
        };
        userInfoListener = new IUiListener() {


            @Override
            public void onComplete(Object o) {
                if (o == null) {
                    return;
                }
                try {
                    JSONObject jo = (JSONObject) o;
                    Log.e("JO:", jo.toString());
                    int ret = jo.getInt("ret");
                    mMNickName = jo.getString("nickname");
                    mFigureurlQq2 = jo.getString("figureurl_qq_2");
                    MeFragment.mFrameLayout.setVisibility(View.GONE);
                    MeFragment.mFl2.setVisibility(View.VISIBLE);
                    MeFragment.mNameTextView.setText(mMNickName);
                    mImageLoader.displayImage(mFigureurlQq2, MeFragment.mPwdRelativeLayout);
                    Log.i("TAG", mMNickName);
                    String gender = jo.getString("gender");
                    Toast.makeText(MainActivity.this, "你好，" + mMNickName, Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                }
            }

            @Override
            public void onError(UiError uiError) {
            }

            @Override
            public void onCancel() {
            }
        };
    }

    //QQ登录回调
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constants.REQUEST_LOGIN) {
            // if (resultCode == -1) {
            // Tencent.onActivityResultData(requestCode, resultCode, data, loginListener);
            Tencent.handleResultData(data, loginListener);
            UserInfo info = new UserInfo(MainActivity.this, mTencent.getQQToken());
            info.getUserInfo(userInfoListener);
            Toast.makeText(MainActivity.this, "走了这个方法", Toast.LENGTH_SHORT).show();
            // }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
